﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Security.Permissions;
using System.Threading;
using sync.Client;
using sync.Library;
using sync.Server;

namespace sync.Present
{
    public partial class frmMain : Form
    {
        SSyncSVtoCL sfc;
        public frmMain()
        {
            InitializeComponent();
            sfc = new SSyncSVtoCL();
        }
        string configFile = "config.ini";
        string configFileTmp = "config1.ini";
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        private void thrdCheck()
        {
          new sFileMethods().checkFileExist(configFile);
            using (StreamReader sr = new StreamReader(configFile))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string path;
                    if (line.StartsWith("path :"))
                    {
                        path = line.Substring(line.LastIndexOf("path :") + 6, line.Length - line.LastIndexOf("path :") - 6);

                    }
                }
            }
        }
        //Load sync path to listview
        private void loadListFolder()
        {
            new sFileMethods().checkFileExist(configFile);
            int count = 0;
            using (StreamReader sr = new StreamReader(configFile))
            {
                string line;
                listFolder.Items.Clear();
                while ((line = sr.ReadLine()) != null)
                {
                    string path;
                    if (line.StartsWith("path :"))
                    {
                        path = line.Substring(line.LastIndexOf("path :") + 6, line.Length - line.LastIndexOf("path :") - 6);

                        listFolder.Items.Add(path, count.ToString());
                        if (Directory.Exists(path))
                        {
                            
                            sClientInfo.pathClient = path;
                          //  watcherFoler(path);
                        }
                        else
                            SMessage.Error=(path + " does not exist !");
                        count++;
                    }
                }
                if (count == 0)
                    SMessage.Error = "File config is empty";
            }

        }
        //show information Notify Icon
        private void showNotify(string s)
        {
            notifyIconSync.ShowBalloonTip(2, "Sync", s, ToolTipIcon.Info);

        }
        private void Main_Load(object sender, EventArgs e)
        {
            Hide();
            this.ShowInTaskbar = false;
            loadListFolder();
        }
        private void btnAddFolder_Click(object sender, EventArgs e)
        {
            new sFileMethods().checkFileExist(configFile);
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    File.AppendAllText(configFile, Environment.NewLine + "path :" + fbd.SelectedPath);
                    loadListFolder();
                    Thread thrd = new Thread(thrdCheck);
                    thrd.Start();
                }
            }
        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (listFolder.Items.Count == 0)
                return;
            new sFileMethods().checkFileExist(configFile);
            string value = listFolder.FocusedItem.Text;
            File.Create(configFileTmp).Close();
            using (StreamReader sr = new StreamReader(configFile))
            {
                string line;
                listFolder.Items.Clear();
                while ((line = sr.ReadLine()) != null)
                {

                    string path;
                    if (line.StartsWith("path :"))
                    {
                        path = line.Substring(line.LastIndexOf("path :") + 6, line.Length - line.LastIndexOf("path :") - 6);
                        if (path != value)
                        {
                            {
                                File.AppendAllText(configFileTmp, Environment.NewLine + path);
                            }
                        }
                    }
                }

            }
            File.Delete(configFile);
            File.Move(configFileTmp, configFile);
            loadListFolder();
            Thread thrd = new Thread(thrdCheck);
            thrd.Start();
        }
        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            if (listFolder.Items.Count == 0 || listFolder.FocusedItem==null)
                return;
            string value = listFolder.FocusedItem.Text;
            {
                System.Diagnostics.Process sdp = new System.Diagnostics.Process();
                sdp.StartInfo.FileName = value;
                sdp.Start();
            }
        }
        //Refresh request server
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //string s = sClientInfo.pathClient;
            SSyncSVtoCL sSyncCL = new SSyncSVtoCL();
            sSyncCL.asyncToClient();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (SSyncSVtoCL.listlogSV.Count == 0)
            {
                lbSync.Visible = false;
                progressBar1.Visible = false;
            }
            else
            {
                lbSync.Visible = true;
                progressBar1.Visible = true;
            }
            if (SMessage.Error != "")
            {
                showNotify(SMessage.Error);
                SMessage.Error = "";
            }
            if (SMessage.Success != "")
            {
                showNotify(SMessage.Success);
                SMessage.Success = "";
            }
        }
        private void resfeshToolStrip_Click(object sender, EventArgs e)
        {
            btnRefresh_Click(null,null);
        }
        //Exit application
        private void exitToolStripMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void notifyIconSync_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            this.Activate();
        }
    }
}
